package com.stud.system.controller.system;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stud.common.annotation.Log;
import com.stud.common.core.controller.BaseController;
import com.stud.common.core.domain.Result;
import com.stud.common.core.page.TableDataInfo;
import com.stud.common.enums.BusinessType;
import com.stud.common.utils.poi.ExcelUtil;
import com.stud.system.domain.SysCacheInit;
import com.stud.system.service.SysCacheInitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 缓存配置Controller
 *
 * @author Stud
 * @date 2023-03-25
 */
@Api(tags = "缓存配置")
@RestController
@RequestMapping("/system/sysCacheInit")
@RequiredArgsConstructor
public class SysCacheInitController extends BaseController {
    private final SysCacheInitService sysCacheInitService;

    @ApiOperation("查询缓存配置列表")
    @PreAuthorize("@ss.hasPermi('system:sysCacheInit:list')")
    @GetMapping("/list")
    public TableDataInfo<SysCacheInit> list(SysCacheInit sysCacheInit) {
        startPage();
        Wrapper<SysCacheInit> wrapper = new QueryWrapper<>(sysCacheInit);
        List<SysCacheInit> list = sysCacheInitService.list(wrapper);
        return getDataTable(list);
    }

    @ApiOperation("导出缓存配置列表")
    @PreAuthorize("@ss.hasPermi('system:sysCacheInit:export')")
    @Log(title = "缓存配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCacheInit sysCacheInit) {
        Wrapper<SysCacheInit> wrapper = new QueryWrapper(sysCacheInit);
        List<SysCacheInit> list = sysCacheInitService.list(wrapper);
        ExcelUtil<SysCacheInit> util = new ExcelUtil<>(SysCacheInit.class);
        util.exportExcel(response, list, "缓存配置数据");
    }

    @ApiOperation("获取缓存配置详细信息")
    @PreAuthorize("@ss.hasPermi('system:sysCacheInit:query')")
    @GetMapping(value = "/{id}")
    public Result<SysCacheInit> getInfo(@PathVariable("id") Long id) {
        return Result.ok(sysCacheInitService.getById(id));
    }

    @ApiOperation("新增缓存配置")
    @PreAuthorize("@ss.hasPermi('system:sysCacheInit:add')")
    @Log(title = "缓存配置", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Boolean> add(@Validated @RequestBody SysCacheInit sysCacheInit) {
        return Result.ok(sysCacheInitService.save(sysCacheInit));
    }

    @ApiOperation("修改缓存配置")
    @PreAuthorize("@ss.hasPermi('system:sysCacheInit:edit')")
    @Log(title = "缓存配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Boolean> edit(@Validated @RequestBody SysCacheInit sysCacheInit) {
        return Result.ok(sysCacheInitService.update(sysCacheInit));
    }

    @ApiOperation("删除缓存配置")
    @PreAuthorize("@ss.hasPermi('system:sysCacheInit:remove')")
    @Log(title = "缓存配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<Boolean> remove(@PathVariable Long[] ids) {
        return Result.ok(sysCacheInitService.removeByIds(ids));
    }

    @ApiOperation(value = "初始化缓存")
    @PostMapping("/cacheInit")
    public Result cacheInit(@RequestParam Long[] ids) {
        List<SysCacheInit> sysCacheInitList = this.sysCacheInitService.listByIds(Arrays.asList(ids));
        for (SysCacheInit cacheInit : sysCacheInitList) {
            sysCacheInitService.initCacheByClassName(cacheInit.getClassName());
        }
        return Result.ok();
    }
}
